Skip to content

Conversation

ityifan
Copy link
Collaborator

@ityifan ityifan commented Sep 17, 2025

This pull request introduces a new mechanism for handling MySQL transactions safely with cache consistency, primarily by adding the MysqlSafeTransaction class and enhancing cache invalidation logic to support transactional safety. The changes ensure that cache invalidation is deferred until after a successful transaction commit, preventing stale data issues in the presence of rollbacks. There are also related adjustments to the MysqlCache class and the public API, as well as a new test/demo for the safe transaction feature.

Safe Transaction & Cache Consistency Improvements:

  • Introduced the MysqlSafeTransaction class, which wraps transactional operations and defers cache invalidation until after a successful commit, using a new mechanism to collect cache namespaces to clear during the transaction. (src/components/MysqlSafeTransaction.ts)
  • Updated the CoaMysql.Transaction type to include __isSafeTransaction and clearCacheNsps properties, enabling the identification and handling of safe transactions throughout the codebase. (src/typings.ts)
  • Enhanced the MysqlCache class so that all cache invalidation operations (deleteCache and related methods) are aware of safe transactions: if a safe transaction is detected, cache invalidation is deferred and the cache namespaces are collected for later deletion. (src/services/MysqlCache.ts) [1] [2] [3]

Cache Read Behavior Adjustments:

  • Modified all cache read methods in MysqlCache to bypass Redis caching when inside a safe transaction, ensuring that reads within a transaction always reflect the most current database state. (src/services/MysqlCache.ts) [1] [2]

API and Test Updates:

  • Updated the public API exports to include MysqlSafeTransaction and reorganized export order for clarity. (src/index.ts)
  • Added a new test/demo file demoSafeTransaction.ts to illustrate and validate the usage of the new safe transaction mechanism. (src/test/demoSafeTransaction.ts)

Minor Refactoring:

  • Minor import order adjustment in MysqlBin.ts for consistency. (src/libs/MysqlBin.ts)

@ityifan ityifan changed the title 修改为删除缓存滞后执行 修复事务同步执行删除缓存问题 Sep 19, 2025
@adaex adaex requested a review from Copilot September 28, 2025 06:07
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a cache synchronization issue in database transactions by implementing safe transaction handling that defers cache deletion until after transaction commit.

  • Introduces MysqlSafeTransaction class to manage transaction-aware cache operations
  • Modifies cache methods to defer cache deletion during transactions and skip cache reads
  • Updates cache deletion logic to accumulate operations during transactions and execute them after commit

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/components/MysqlSafeTransaction.ts New class that wraps transactions with cache-aware handling
src/services/MysqlCache.ts Modified cache methods to detect safe transactions and defer cache operations
src/index.ts Added export for MysqlSafeTransaction and reorganized imports
src/libs/MysqlBin.ts Minor import reordering
src/test/demoSafeTransaction.ts Test file demonstrating the new safe transaction functionality
Comments suppressed due to low confidence (1)

src/services/MysqlCache.ts:1

  • The variable name clearCacheNsps should be more descriptive, such as cacheNamespacesToClear or pendingCacheDeletes.
import { CoaError } from 'coa-error'

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Member

@adaex adaex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@adaex adaex changed the title 修复事务同步执行删除缓存问题 feat: add support safe transaction Sep 28, 2025
@ityifan ityifan merged commit 68c1d30 into main Oct 9, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants